package pers.hl.module_main

import android.os.Bundle
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.common.base.BaseVBVMFragment
import pers.hl.lib_global.model.bean.Book
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.databinding.FragmentBaseTitleRvBinding
import pers.hl.module_main.mine.MineViewModel

abstract class BaseTitleRvFragment<T> :
    BaseVBVMFragment<FragmentBaseTitleRvBinding, MineViewModel>() {

    protected lateinit var mAdapter: BaseLibQuickAdapter<T, BaseViewHolder>

    override fun initView(savedInstanceState: Bundle?) {
        initTitle()
        initRV()
    }

    private fun initTitle() {
        mBinding.apply {
            titleBar.apply {
                ivBarLeft.setOnClickListener {
                    killSelf()
                }
                tvBarTitle.text = getTitle()
            }
        }
    }

    open fun killSelf() {
        activity?.finish()
    }

    abstract fun getTitle(): String

    private fun initRV() {
        mBinding.apply {
            recyclerView.layoutManager = LinearLayoutManager(mContext)
            mAdapter = createAdapter()
            beforeSetAdapter()
            recyclerView.adapter = mAdapter
            hideUnnecessaryMenu()
        }
    }

    abstract fun beforeSetAdapter()

    abstract fun createAdapter(): BaseLibQuickAdapter<T, BaseViewHolder>

    override fun useVMReflect(): Boolean {
        return false
    }

    override fun initViewModel(): MineViewModel {
        return ViewModelProvider(this).get(MineViewModel::class.java)
    }

    override fun initViewBinding(container: ViewGroup?): FragmentBaseTitleRvBinding {
        return FragmentBaseTitleRvBinding.inflate(layoutInflater, container, false)
    }

    override fun useVBReflect(): Boolean {
        return false
    }

    open fun hideUnnecessaryMenu() {

    }

    protected fun hideMenu(index: Int) {
        mBinding.recyclerView.setSwipeItemMenuEnabled(index, false)
    }

}